package com.dq.health.utils.view;

import java.util.ArrayList;
import java.util.List;

public class PageUtil {

	/**
	 * 
	 * @param pageSize
	 *            页大小
	 * @param pageIndex
	 *            页码
	 * @param list
	 * @return
	 */
	public static <T> List<T> subList(int pageSize, int pageIndex, List<T> list) {
		if (list == null || list.size() == 0) {
			return new ArrayList<>();
		}
		// 总数量
		int totalRecord = list.size();

		// 总页数
		int totalPage = totalRecord % pageSize == 0 ? totalRecord / pageSize : totalRecord / pageSize + 1;

		// 当前页数
		int currentPage = totalPage < pageIndex ? totalPage : pageIndex;

		// 起始索引
		int fromIndex = pageSize * (currentPage - 1);

		// 结束索引
		int toIndex = pageSize * currentPage > totalRecord ? totalRecord : pageSize * currentPage;

		return list.subList(fromIndex, toIndex);
	}
}
